﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using EIA.PM.Model;
using EIA.Basic.Model;
using EIA.Basic.BLL.ClientProxy;

namespace EIA.SLClient.PlatManage.PMMgr
{
    public class EngineerEditViewModel : SimpleMvvmToolkit.ViewModelDetailBase<EngineerEditViewModel, EIA.PM.Model.TEngineer>
    {
        public EngineerEditViewModel()
        {
            IsShow = false;
            if (IsInDesignMode)
            {
                IsShow = true;
                return;
            }

            this.RegisterToReceiveMessages<TEngineer>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ENGINEER_UPDATE, EP_Update);
            this.RegisterToReceiveMessages<TEngineer>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ENGINEER_NEW, EP_New);
            TryLoadStaffCollection();
            TryLoadIndustryCollection();
        }

        private void EP_Update(object sender, SimpleMvvmToolkit.NotificationEventArgs<TEngineer> e)
        {
            TryLoadStaffCollection();
            TryLoadIndustryCollection();
            this.Model = e.Data;
            this.TitleTip = "修改";
            this.BeginEdit();
            SelectIndustry();
            SelectStaff();
            this.IsShow = true;
            this.IsNew = false;
        }

        private void EP_New(object sender, SimpleMvvmToolkit.NotificationEventArgs<TEngineer> e)
        {
            TryLoadStaffCollection();
            TryLoadIndustryCollection();
            this.Model = e.Data;
            this.TitleTip = "添加";
            this.BeginEdit();
            SelectIndustry();
            SelectStaff();
            this.IsShow = true;
            this.IsNew = true;
        }


        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ENGINEER_BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ENGINEER_BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected string _TitleTip;
        /// <summary>
        /// 注释
        /// </summary>
        public string TitleTip
        {
            get { return _TitleTip; }
            set
            {
                _TitleTip = value;
                NotifyPropertyChanged(m => m.TitleTip);
            }
        }

        protected bool _IsShow;
        /// <summary>
        /// 是否显示
        /// </summary>
        public bool IsShow
        {
            get { return _IsShow; }
            set
            {
                _IsShow = value;
                NotifyPropertyChanged(m => m.IsShow);
            }
        }

        protected bool _IsNew;
        /// <summary>
        /// 是否为添加操作
        /// </summary>
        public bool IsNew
        {
            get { return _IsNew; }
            set
            {
                _IsNew = value;
                NotifyPropertyChanged(m => m.IsNew);
            }
        }

        protected System.Collections.ObjectModel.ObservableCollection<DicItem> _IndustryCollection;
        /// <summary>
        /// 注释
        /// </summary>
        public System.Collections.ObjectModel.ObservableCollection<DicItem> IndustryCollection
        {
            get { return _IndustryCollection; }
            set
            {
                _IndustryCollection = value;
                NotifyPropertyChanged(m => m.IndustryCollection);
            }
        }

        protected PagedCollectionView _StaffCollection;
        /// <summary>
        /// 注释
        /// </summary>
        public PagedCollectionView StaffCollection
        {
            get { return _StaffCollection; }
            set
            {
                _StaffCollection = value;
                NotifyPropertyChanged(m => m.StaffCollection);
            }
        }

        protected TStaff _SelectedStaff;
        /// <summary>
        /// 注释
        /// </summary>
        public TStaff SelectedStaff
        {
            get { return _SelectedStaff; }
            set
            {
                _SelectedStaff = value;
                NotifyPropertyChanged(m => m.SelectedStaff);
            }
        }

        protected DicItem _SelectedIndustry;
        /// <summary>
        /// 注释
        /// </summary>
        public DicItem SelectedIndustry
        {
            get { return _SelectedIndustry; }
            set
            {
                _SelectedIndustry = value;
                NotifyPropertyChanged(m => m.SelectedIndustry);
            }
        }

        protected string _StaffFilter;
        /// <summary>
        /// 注释
        /// </summary>
        public string StaffFilter
        {
            get { return _StaffFilter; }
            set
            {
                _StaffFilter = value;
                NotifyPropertyChanged(m => m.StaffFilter);
                if(StaffCollection != null)StaffCollection.Refresh();
            }
        }

        #endregion

        #region command

        #region SaveCommand

        private SimpleMvvmToolkit.DelegateCommand _SaveCommand = null;
        public SimpleMvvmToolkit.DelegateCommand SaveCommand
        {
            get
            {
                if (_SaveCommand == null)
                {
                    _SaveCommand = new SimpleMvvmToolkit.DelegateCommand(Save, CanExcuteSave);
                } return _SaveCommand;
            }
            set { _SaveCommand = value; NotifyPropertyChanged(m => m.SaveCommand); }
        }

        public void Save()
        {
            EIA.PM.BLL.ClientProxy.TEngineerProxy proxy = new EIA.PM.BLL.ClientProxy.TEngineerProxy();
            Busy();
                UpdateIndustry();
                UpdateStaff();
            if (IsNew) proxy.Insert(Model, (k) =>
            {
                Free();
                Model.EngId = k;
                this.EndEdit();
                this.TitleTip = "";
                this.IsShow = false;
                this.Model = null;
            }, (ex) =>
            {
                Free();
                this.TitleTip = "添加 (发生错误)";
            });
            else proxy.Update(Model, (r) =>
            {
                Free();
                if (r)
                {
                    this.EndEdit();
                    this.TitleTip = "";
                    this.IsShow = false;
                    this.Model = null;
                }
                else
                {
                    this.TitleTip = "修改 (数据库更新失败)";
                }
            }, (ex) =>
            {
                Free();
                this.TitleTip = "修改 (发生错误)";
            });

        }

        public bool CanExcuteSave()
        {
            return true;
        }

        #endregion

        #region CancelCommand

        private SimpleMvvmToolkit.DelegateCommand _CancelCommand = null;
        public SimpleMvvmToolkit.DelegateCommand CancelCommand
        {
            get
            {
                if (_CancelCommand == null)
                {
                    _CancelCommand = new SimpleMvvmToolkit.DelegateCommand(Cancel, CanExcuteCancel);
                } return _CancelCommand;
            }
            set { _CancelCommand = value; NotifyPropertyChanged(m => m.CancelCommand); }
        }

        public void Cancel()
        {
            this.CancelEdit();
            if (this.IsNew) SendMessage<TEngineer>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ENGINEER_NEW_CANCEL, new SimpleMvvmToolkit.NotificationEventArgs<TEngineer>("", Model));
            this.TitleTip = "";
            this.IsShow = false;
            this.Model = null;
            //if(this.IsNew) SendMessage(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ENGINEER_RETRIEVE, new SimpleMvvmToolkit.NotificationEventArgs());
        }

        public bool CanExcuteCancel()
        {
            return true;
        }

        #endregion

        #endregion

        private void LoadStaffCollection()
        {
            TStaffProxy proxy = new TStaffProxy();
            proxy.ListAll((lst) =>
            {
                StaffCollection = new PagedCollectionView(lst); 
                StaffCollection.Filter = (f) => { return _StaffFilter == null ? true : (f as TStaff).StfName.Contains(_StaffFilter); };
            }, (ex) =>
            {

            });
        }

        private void LoadIndustryCollection()
        {
            XDictProxy proxy = new XDictProxy();
            proxy.List("HYLX", int.MaxValue, 1, (lst) =>
            {
                IndustryCollection = new System.Collections.ObjectModel.ObservableCollection<DicItem>(lst);
            }, (ex) =>
            {

            });
        }

        public void TryLoadStaffCollection()
        {
            if (StaffCollection == null) LoadStaffCollection();
        }

        public void TryLoadIndustryCollection()
        {
            if (IndustryCollection == null) LoadIndustryCollection();
        }

        private void SelectStaff()
        {
            if (Model == null) SelectedStaff = null;
            if(StaffCollection != null && StaffCollection.SourceCollection is IList<TStaff>)
                SelectedStaff = (StaffCollection.SourceCollection as IList<TStaff>).FirstOrDefault((s) => { return s.StfId == Model.StfId; });
        }

        private void SelectIndustry()
        {
            if (Model == null) SelectedIndustry = null;
            if (IndustryCollection != null) SelectedIndustry = IndustryCollection.FirstOrDefault((i) => { return i.DicIid == Model.EngType; });
        }

        private void UpdateStaff()
        {
            Model.StfId = SelectedStaff == null ? -1 : SelectedStaff.StfId;
            Model.StfName = SelectedStaff == null ? null : SelectedStaff.StfName;
        }

        private void UpdateIndustry()
        {
            Model.EngType = SelectedIndustry == null ? -1 : SelectedIndustry.DicIid;
            Model.EngAlias = SelectedIndustry == null ? null : SelectedIndustry.DicIname;
        }
    }
}